package com.luxf.base.domainevent.utils;

import com.luxf.base.domainevent.annotation.AggregateRoot;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: 待完善.
 */
@AggregateRoot
public class DomainEventSupportable {
    private final List<Object> domainEvents = new ArrayList<>();

    public void addEvent(Object event) {
        domainEvents.add(event);
    }

    public void removeEvent(Object event) {
        domainEvents.remove(event);
    }

    public void publishEvents() {
        SpringContextUtils.getContext()
                .ifPresent(context ->
                        this.domainEvents.forEach(context::publishEvent));
    }
}
